/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk> and Matthias Buch-Kromann <mbk.isv@cbs.dk>
 *
 *  This file is part of the IncrementalParser package.
 *
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.incparser.data;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Haulrich
 */
public class ConllIterator implements Iterator<CONLLSentence> {
    final String fn;
    CONLLSentence nextSen;
    BufferedReader input;
    boolean hasNext;

    public ConllIterator(String fn) throws FileNotFoundException, UnsupportedEncodingException {
        this.fn = fn;
        open();
    }

    public boolean hasNext() {
        return hasNext;
    }

    public CONLLSentence next() {
        CONLLSentence sentence = nextSen;
        update();
        return sentence;
    }

    private void update() {
        try {
            hasNext = true;
            boolean linesRead = false;

            List<CONLLToken> words = new ArrayList<CONLLToken>();
            String line;

            while ((line = input.readLine()) != null) {
                linesRead = true;
                if (line.isEmpty()) {
                    nextSen = new CONLLSentence(words);
                    return;
                } else {
                    String[] tokens = line.split("\t");
                    List<String> lTokens = Arrays.asList(tokens);
                    words.add(new CONLLToken(lTokens));

                }
            }
            if (!linesRead) {
                hasNext = false;
            }

        } catch (IOException ex) {
            Logger.getLogger(ConllIterator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void reset() throws IOException {
        close();
        open();

    }

    private void open() throws FileNotFoundException, UnsupportedEncodingException {
        input = new BufferedReader(new InputStreamReader(new FileInputStream(fn), "UTF8"));
//        input = new BufferedReader(new FileReader(fn), "UTF8");
        update();
    }

    public void close() throws IOException {
        input.close();
    }

    public void remove() {
        throw new UnsupportedOperationException("Not supported.");
    }
}
